﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using Ewts.Code;

namespace Ewts.RDLC
{
    public partial class SegregationRDLC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string instorageID = Request["instorageID"].ToString().Trim();
                DataBindDrop(instorageID);
                Search(instorageID, "");
            }

            
        }
       

        protected void Search(string insid,string indate )
        {
            DataLib.Model.InStorage insModel = new DataLib.Model.InStorage();
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            List<ReportParameter> parameterList = new List<ReportParameter>();
            DataLib.Model.LoadNum load = new DataLib.Model.LoadNum();
            if (Request["instorageID"] != null && Request["instorageID"].ToString().Trim() != "")
            {
                insModel = new DataLib.BLL.InStorage().GetModel(int.Parse(insid));
            }
            parameterList.Add(new ReportParameter("InstorageNo", insModel.wrlno));

            if (insModel.outid != 0)
            {
                outModel = new DataLib.BLL.OutStorage().GetModel((int)insModel.outid);
                parameterList.Add(new ReportParameter("LoadNum", outModel.VLoadNum));
                parameterList.Add(new ReportParameter("CollectDate", ((DateTime)outModel.outStorageDate).ToLongDateString().ToString()));
                DataLib.Model.Vload vmodel = new DataLib.Model.Vload();
                DataLib.BLL.Vload vbll = new DataLib.BLL.Vload();
                vmodel = vbll.GetModelByOutID((int)insModel.outid);
                string cum = "";
                if (outModel.VLoadNum.Substring(0, 2) == "86")
                {
                    load = new DataLib.BLL.LoadNum().GetLoadID(outModel.VLoadNum);
                    if (load.printname != null && load.printname.Trim() != "")
                        cum = load.printname;
                    else
                        cum = load.cumname;
                }
                else if (vmodel != null)
                {
                    cum = vmodel.inscumname;
                }
                parameterList.Add(new ReportParameter("Customer", cum));
            }
            else if (insModel.cjoid != 0)
            {
                DataLib.Model.Cjo cmodel = new DataLib.BLL.Cjo().GetModel((int)insModel.cjoid);
                if (cmodel != null)
                {
                    parameterList.Add(new ReportParameter("Customer", cmodel.customername));
                    parameterList.Add(new ReportParameter("CollectDate", ((DateTime)cmodel.cjocolldate).ToLongDateString().ToString()));
                    parameterList.Add(new ReportParameter("LoadNum", cmodel.loadnum));
                }
            }


            parameterList.Add(new ReportParameter("HandleDate", (DateTime.Parse(DropDownList1.SelectedValue)).ToLongDateString().ToString()));


            string time1 = "";
            string time2 = "";
            if (indate != "" && indate.Trim() != "")
            {
                time1 = indate;
                time2 = indate;
            }
            DataLib.BLL.InStorage bll = new DataLib.BLL.InStorage();
            DataTable dt = bll.GETItemsByInsNo(time1, time2,insModel.wrlno);
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            new RDLCPrinter().FillRow(dt, 3, 15, 15);
            ReportViewer1.LocalReport.SetParameters(parameterList);
            ReportViewer1.LocalReport.Refresh();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string wrlid = Request["instorageID"].ToString().Trim();
            Search(wrlid,DropDownList1.SelectedValue);
        }


        public void DataBindDrop(string wrlid)
        {
            // 给DropDownList1绑定日期  -
            DataSet ds = new DataLib.BLL.BarCode().GetList("InStrorageId='" + wrlid + "'");
            List<string> lt = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                if (lt.Contains(Convert.ToDateTime(ds.Tables[0].Rows[i]["createdate"]).ToShortDateString()))
                {
                    continue;
                }
                else
                {
                    string date = Convert.ToDateTime(ds.Tables[0].Rows[i]["createdate"]).ToShortDateString();
                    lt.Add(date);
                    DropDownList1.Items.Add(date);
                }

            }
        }
    }
}